﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.Util;
using ZLCMS.Data.Model;
using NHibernate;
using NHibernate.Criterion;
using DotNet.Common.NHibernateUtil;

namespace ZLCMS.Web.MemberUser
{
    public partial class MemberMaster : System.Web.UI.MasterPage
    {
        public MemberInfo _memberInfo;

        protected void Page_Load(object sender, EventArgs e)
        {
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                        AdsKits.BindAdsListByPositionCode(rpt_activity, session, "Search", -1);
                }
        }
        public MemberInfo LoadMember()
        {
            string account = Session[GlobalSessionString.CurrentMemberAccount].ToString();

            if (account == null)
            {
                Response.Redirect("/MemberUser/Login.aspx");
            }

            try
            {
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    _memberInfo = session.CreateCriteria<MemberInfo>().Add(Expression.Eq("AccountId", account)).UniqueResult<MemberInfo>();
                }
            }
            catch
            {
                Response.Redirect("/MemberUser/Login.aspx");
            }

            if (_memberInfo == null)
            {
                Response.Redirect("/MemberUser/Login.aspx");
            }
            else if (_memberInfo.Status != "1")
            {
                Response.Redirect("/");
            }

            return _memberInfo;
        }

        //public string GetSearchCategoryItemHtml(int categoryId)
        //{
        //        string htmStr = "";
        //        using (ISession session = SessionFactoryHelper.CreateSession())
        //        {
        //                ShopCategory category = session.CreateCriteria<ShopCategory>().Add(Expression.Eq("Id", categoryId)).UniqueResult<ShopCategory>();

        //                if (category == null)
        //                {
        //                        return "";
        //                }
        //                htmStr = "<a href='/Product/ProductList.aspx?categoryid=" + categoryId + "' >" + category.CategoryName + "</a><div class='wrap'>";

        //                IList<ShopCategorySearchProperty> propertyList = session.CreateCriteria<ShopCategorySearchProperty>().Add(Expression.Eq("CategoryId", categoryId)).List<ShopCategorySearchProperty>();
        //                foreach (ShopCategorySearchProperty property in propertyList)
        //                {
        //                        htmStr += "<p><span>" + property.PropertyValue + "</span></p> <ul class='wrap_list'>";
        //                        IList<ShopCategorySearchPropertyItem> propertyItemList = session.CreateCriteria<ShopCategorySearchPropertyItem>().Add(Expression.Eq("SearchPropertyId", property.Id)).List<ShopCategorySearchPropertyItem>();
        //                        for (int i = 0; i < propertyItemList.Count; i++)
        //                        {
        //                                htmStr += "<li ";
        //                                if (i == 0)
        //                                {
        //                                        htmStr += " class='fir'";
        //                                }
        //                                htmStr += "><a href='/Product/ProductList.aspx?categoryid=" + category.Id + "&" + property.Id + "=" + propertyItemList[i].Id + "'>" + propertyItemList[i].PropertyName + "</a></li>";
        //                        }
        //                        htmStr += "<div class='clear'></div></ul>";
        //                }
        //                htmStr += "  </div>";
        //                return htmStr;
        //        }
        //}

        public string GetMainNav()
        {
            string htmStr = "";
            using (NHibernate.ISession session = SessionFactoryHelper.CreateSession())
            {
                CmsTreeDef rtreeDef = session.CreateQuery("from CmsTreeDef where Kind='PcMainNav' and Parent='0' ").UniqueResult<CmsTreeDef>();
                IList<CmsTreeDef> treeDefList = session.CreateQuery("from CmsTreeDef where Kind='PcMainNav' and Parent='" + rtreeDef.Id + "' ").List<CmsTreeDef>();
                foreach (CmsTreeDef t in treeDefList)
                {
                    htmStr += " <li><a href='" + t.Value + "' >" + t.Name + "</a></li>";
                }
            }
            return htmStr;

        }
    }
}